<?php

namespace app\api\controller;

use app\admin\model\Villages as VillagesMdl;
use app\admin\model\Villnavigationdetail;
use app\admin\model\Image;
use app\admin\model\Article;
use app\admin\model\Operator;
use app\admin\model\Member;

use think\Request;

/**
 * 村子
 */
class Villages extends BaseController
{

    /**
     * 获取村子列表
     *
     * @return \think\response\Json
     */
    public function getVillagesList()
    {
        $param = self::getHttpParam();
        $start = 0;
        $length = 20;

        if (empty($param->townId)) {
            return show(config('status.ERROR_STATUS'), self::NOT_PARAM, 'townId 不能为空');
        }
        if (!empty($param->start)) {
            $start = $param->start;
        }
        if (!empty($param->length)) {
            $length = $param->length;
        }

        $where = [
            'townId' => $param->townId
        ];

        $model = new VillagesMdl;
        $fields = 'id, name, imgUrl';
        $list = $model->where($where)->limit($start, $length)->field($fields)->select();
        $total = $model->where($where)->count();

        if (empty($list)) {
            return show(config('status.ERROR_STATUS'), self::NOT_DATA, '未查到村子');
        }

        return showTotal(config('status.SUCCESS_STATUS'), self::MSG_SUCCESS, $list, $total);
    }

    /**
     * 获取村子详情
     *
     * @return \think\response\Json
     */
    public function getVillagesDetail()
    {
        $param = self::getHttpParam();

        if (empty($param->townId)) {
            return show(config('status.ERROR_STATUS'), self::NOT_PARAM, 'townId 不能为空');
        }
        if (empty($param->villageId)) {
            return show(config('status.ERROR_STATUS'), self::NOT_PARAM, 'villageId 不能为空');
        }
        if (empty($param->userId)) {
            return show(config('status.ERROR_STATUS'), self::NOT_PARAM, 'userId 不能为空');
        }

        $where = [
            'townId' => $param->townId,
            'villageId' => $param->villageId
        ];

        // 村子详情
        $villageInfo = VillagesMdl::where(['id' => $param->villageId])->field('id, name, imgUrl, address, addressGeo, introduction')->find();
        // 村子图集
        $imagesList = Image::where(['relatedId' => $villageInfo->id, 'tag' => 'imglist'])->where('imgUrl', 'neq', $villageInfo->imgUrl)->field('imgUrl')->order('sorting ASC')->select();
        $villageInfo['imagesList'] = $imagesList;

        // 公告栏
        $articleNotice = $this->getArticleNotice($where);

        $data = [
            'illageInfo' => $villageInfo,
            'articleNotice' => $articleNotice ? : '',
        ];

        return show(config('status.SUCCESS_STATUS'), self::MSG_SUCCESS, $data);
    }

    /**
     * 获取公告栏
     */
    protected function getArticleNotice($where)
    {
        $model = new Article;
        //$model = db('article');
        $where['villageId'] = ['in', ['0', $where['villageId']]];
        $where['type'] = 'notice';
        $field = 'id, iconUrl, title, createDate, createOper, readCount, commentCount';
        $result = $model->where($where)->field($field)->limit(0, 5)->order('isRecommend ASC,orderNo ASC,createDate DESC')->select();
        if (!$result) {
            return false;
        }

        return $result;
    }
}